package com.ctl.test;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class PageServlet extends HttpServlet {
	public PageServlet() {
		super();
	}

	public void destroy() {
		super.destroy();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String sql = request.getParameter("sql");
		SelectMethod db = new SelectMethod();
		List<Student> list_student = db.getStudents(sql);
		PageBean pagebean=new PageBean(list_student.size());//初始化pageBean对象
		pagebean.setCurPage(1);//设置当前页
		int pagesize=pagebean.getPageSize();//获取分页大小
		//获取分页数据在List集合中的索引
		int firstIndex=0;
		int toIndex=1*pagesize;
		if(toIndex>list_student.size()){
			toIndex=list_student.size();
		}
		if(firstIndex>toIndex){
			firstIndex=0;
			pagebean.setCurPage(1);
		}
		//截取数据集合，获取分页数据
	    List<Student> list_student_cut=list_student.subList(firstIndex, toIndex);
		request.getSession().setAttribute("pageBean", pagebean);
		request.getSession().setAttribute("list_student", list_student_cut);
		response.sendRedirect("index.jsp");
	}

	public void init() throws ServletException {
		// Put your code here
	}

}
